package com.sgb.kjwl.viewmodel.packers;


import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.AreaCodeEntity;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.packers.PackersSearchActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author wangyajun
 * @date 2020/9/10
 * description:
 */
public class PackersSearchModel extends BaseViewModel {
    private PackersSearchActivity activity;
    private HomeRepertory repertory;

    public PackersSearchModel(PackersSearchActivity activity, HomeRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getMenuList() {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getPackerMenuList().enqueue(new Callback<BaseEntity<List<MenuEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MenuEntity>>> call, Response<BaseEntity<List<MenuEntity>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    DialogHelper.dismissProgressDialog();
                    activity.showEmpty();
                    return;
                }

                MToast.showToast(activity, response.body().getMsg());
                DialogHelper.dismissProgressDialog();
                activity.showEmpty();

            }

            @Override
            public void onFailure(Call<BaseEntity<List<MenuEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                activity.showEmpty();
            }
        });
    }

    public void getTeamHotKeySearch() {
        AreaCodeEntity areaCodeEntity = new AreaCodeEntity();
        areaCodeEntity.setAreaCode(repertory.getAreaCode());
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getTeamHotKeySearch(areaCodeEntity).enqueue(new Callback<BaseEntity<List<String>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<String>>> call, Response<BaseEntity<List<String>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.initHot(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                    activity.showEmpty();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<String>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }

        });
    }

    /**
     * 存储历史搜索记录
     */
    public void setShardHistorySearch(String content) {
        StringBuilder builder = new StringBuilder();
        SharedPreferenceUtil util = SharedPreferenceUtil.getInstance();
        String hsValue = util.getSaveStringData(SharedPreferenceUtil.HISTORY_SEARCH, "");
        if (!TextUtils.isEmpty(hsValue)) {
            if (!hsValue.contains(content)) {
                builder.append(hsValue).append("#").append(content);
            } else {
                builder.append(hsValue);
            }
        } else {
            builder.append(content);
        }
        util.saveData(SharedPreferenceUtil.HISTORY_SEARCH, builder.toString());
    }

    /**
     * 获取历史搜索记录集合
     */
    public List<String> getHistorySearchList() {
        List<String> hsValueList = new ArrayList<>();
        SharedPreferenceUtil util = SharedPreferenceUtil.getInstance();
        String hsValue = util.getSaveStringData(SharedPreferenceUtil.HISTORY_SEARCH, "");
        if (TextUtils.isEmpty(hsValue)) {
            return null;
        }
        if (hsValue.contains("#")) {
            String[] hsValues = hsValue.split("#");
            Collections.addAll(hsValueList, hsValues);
        } else {
            hsValueList.add(hsValue);
        }
        return hsValueList;
    }

    /**
     * 删除搜索记录集合
     */
    public boolean clearHistorySearchData() {
        SharedPreferenceUtil util = SharedPreferenceUtil.getInstance();
        util.saveData(SharedPreferenceUtil.HISTORY_SEARCH, "");
        String hsValue = util.getSaveStringData(SharedPreferenceUtil.HISTORY_SEARCH, "");
        return TextUtils.isEmpty(hsValue);
    }
}
